package server.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import server.bean.Goods;
import server.bean.Img;
import server.bean.Page;
import server.bean.Shop;
import server.mapper.ShopMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@RestController
public class ShopController {
    @Autowired ShopMapper shopMapper;

    @PostMapping("/shop")
    public PageInfo<Shop> getShop(@RequestParam Integer page,@RequestParam Integer pageSize){
        PageHelper.startPage(page, pageSize);
        List<Shop> list = shopMapper.selectShop();
        PageInfo<Shop> pageInfo = new PageInfo<>(list,5);
        return pageInfo;
    }
    @RequestMapping("/insertImage")
    @ResponseBody
    public String insertImage(@RequestBody MultipartFile file){
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        try {
            InputStream is = file.getInputStream();
            Img img = new Img();
            int fileSize = (int)file.getSize();
            System.out.println(fileSize);
            byte[] productImage = new byte[fileSize];
            is.read(productImage);
            img.setProductImage(productImage);
            shopMapper.insertImage(img);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/updataImage")
    @ResponseBody
    public String updataImage(@RequestBody MultipartFile file, @RequestParam Integer goodsImgId){
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        try {
            InputStream is = file.getInputStream();
            Img img = new Img();
            int fileSize = (int)file.getSize();
            System.out.println(fileSize);
            byte[] productImage = new byte[fileSize];
            is.read(productImage);
            img.setProductImage(productImage);
            img.setGoodsImgId(goodsImgId);
            shopMapper.updataImage(img);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @GetMapping("/getPhoto")
    public void getPhotoById(final HttpServletResponse response,@RequestParam Integer goodsImgId) throws IOException {
        Img img = shopMapper.getImage(goodsImgId);
        response.setContentType("image/jpeg");
        response.setCharacterEncoding("UTF-8");
        int len = 0;
        byte[] buf = new byte[1024];
        OutputStream outputStream = response.getOutputStream();
            byte[] data = img.getProductImage();
            InputStream in = new ByteArrayInputStream(data);
            while ((len = in.read(buf,0 ,buf.length)) != -1) {
                outputStream.write(buf, 0, len);
            }
        outputStream.close();
    }
    @GetMapping("/selectGoods")
    public List<Goods> selectGoods(@RequestParam Integer belongId){
         return shopMapper.selectGoods(belongId);

    }

}
